import os
import sys
from audio_io.input import AudioInput
from audio_io.output import AudioOutput
from tts.engine import TTSEngine
from nlp.processor import NLPProcessor
from dialog.manager import DialogManager

class VoiceAssistant:
    def __init__(self):
        self.audio_input = AudioInput()
        self.audio_output = AudioOutput()
        self.tts_engine = TTSEngine()
        self.nlp_processor = NLPProcessor()
        self.dialog_manager = DialogManager()

    def run(self):
        print("语音助手启动中...")
        while True:
            try:
                # 1. 获取音频输入
                audio_data = self.audio_input.listen()
                
                # 2. 语音识别
                text = self.audio_input.recognize(audio_data)
                print(f"用户说: {text}")
                
                # 3. NLP处理
                processed_text = self.nlp_processor.process(text)
                
                # 4. 对话管理
                response = self.dialog_manager.handle(processed_text)
                
                # 5. 语音合成
                audio_response = self.tts_engine.synthesize(response)
                
                # 6. 音频输出
                self.audio_output.play(audio_response)
                
            except KeyboardInterrupt:
                print("\n语音助手已关闭")
                sys.exit(0)
            except Exception as e:
                print(f"发生错误: {e}")
                continue

if __name__ == "__main__":
    assistant = VoiceAssistant()
    assistant.run()